package com.jy.event.pay.request;

import com.jy.event.util.SignUtil;
import com.jy.event.util.SignUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * Created by lihu on 2017/10/9.
 * 威副通支付请求实体
 */
@Data
@XStreamAlias("xml")
@NoArgsConstructor
@AllArgsConstructor
public class PayRequest {


    @XStreamAlias("sign")
    private String sign;

    @XStreamAlias("nonce_str")
    private String nonce_str = String.valueOf(new Date().getTime());

    //接口类型
    @XStreamAlias("service")
    private String service;

//    @XStreamAlias("charset")
//    private String charset = "UTF-8";

    //商户号
    @XStreamAlias("mch_id")
    private String mch_id;

    //商户订单号
    @XStreamAlias("out_trade_no")
    private String out_trade_no;

    //商品信息
//    @XStreamAlias("body")
//    private String body;

    //总金额
//    @XStreamAlias("total_fee")
//    private int total_fee;

    // 终端ip
    @XStreamAlias("mch_create_ip")
    private String mch_create_ip;

    //通知地址
    @XStreamAlias("notify_url")
    private String notify_url;

    //获取用户的openid
    @XStreamAlias("sub_openid")
    private String sub_openid;

    //商户的微信公众号ID
    @XStreamAlias("sub_appid")
    private String sub_appid;


    //授权码
    @XStreamAlias("auth_code")
    private String auth_code;

    //是否原生
//    @XStreamAlias("is_raw")
//    private int is_raw;

    //附加信息
//    @XStreamAlias("attach")
//    private int attach;

    //退单号
    @XStreamAlias("out_refund_no")
    private String out_refund_no;

    //总金额
//    @XStreamAlias("refund_fee")
//    private int refund_fee;

    //总金额
    @XStreamAlias("op_user_id")
    private String op_user_id;

    //总金额
    @XStreamAlias("transaction_id")
    private String transaction_id;

    //账单类型
    @XStreamAlias("bill_type")
    private String bill_type = "ALL";

    //账单日期
    @XStreamAlias("bill_date")
    private String bill_date;


//    @XStreamAlias("sign_type")
//    private String sign_type;
//    @XStreamAlias("sign_agentno")
//    private String sign_agentno;

    public PayRequest(String service, String mch_id, String outTradeNo) {
        this.service = service;
        this.mch_id = mch_id;
        this.out_trade_no = outTradeNo;
        //this.sign_agentno = "10100000284";
    }

    public PayRequest(String service, String mch_id) {
        this.service = service;
        this.mch_id = mch_id;
    }


    //实体转化 xml
    public String toXML() {
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(this.getClass());
        return xstream.toXML(this);
    }

    //设置sign
    //直接使用威福通dome
    //todo 后期可优化
    public void setUpSign(Map<String, String> map, String key) throws Exception {
        Map<String, String> params = SignUtils.paraFilter(map);
        //key="h92Tz0wF6QX66hR4pM7VVdXD4NNS3cSO";
        /*if (StringUtils.isNotBlank(key)) {
            sign_type = "MD5";
            params.put("sign_type", "MD5");
        } else {
            sign_type = "RSA_1_256";
            params.put("sign_type", "RSA_1_256");
        }*/
        StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
        SignUtils.buildPayParams(buf, params, false);
        String preStr = buf.toString();
        System.out.println(preStr);
        this.sign = SignUtil.getSign("MD5", preStr, key).toUpperCase();
    }


}
